<?php
class Admin_BankAccountsController extends Model3_Controller
{
    private $_credentials;
   // protected $_config;
    
    public function init()
    {
        if(!Model3_Auth::isAuth())
            $this->redirect();
        else
        {
            $role = Model3_Auth::getCredentials('user_type');
            if( $role !== DefaultDb_Entity_User::TYPE_ADMIN )
            {
               Model3_Auth::deleteCredentials();
               $this->redirect();
            }
        }
        $this->_credentials = Model3_Auth::getCredentials();
        $this->view->setTemplate('Admin');
    }
    
    public function indexAction()
    {
    	
    }
    
    public function frmBankAccountAction()
    {
    	$em = $this->getEntityManager('DefaultDb');
    	$lista = $em->getRepository('DefaultDb_Entity_State')->findAll();
    	 
    	$combo = "";
    	foreach($lista as $item)
    	{
    		$combo .="<option value='".$item->getId(); 
    		$combo .="'>".$item->getName()."</option>\n";
    	}
    	
    	$this->view->states = $combo;
    	
    }
    
    public function saveBankAccountAction()
    {
    	$this->view->setUseTemplate(FALSE);
    	header("Content-Type: application/json");
    	
    	$post= $this->getRequest()->getPost();

    	$em= $this->getEntityManager('DefaultDb');
    	$user= $em->getRepository('DefaultDb_Entity_User')->findOneBy(array('id_users' => $this->_credentials));
    	
    	/*		if ($action=="btnSaveAccount")
    			{*/
    				$count= new DefaultDb_Entity_BankAccount();
    	/*			}
    			
    			if ($action=="btnEditAccount")
    	
    			{
    				$count= ($id_bank_account) ? $em->getRepository('DefaultDb_Entity_BankAccount')->findOneBy(array('id_bank_account' =>$id_bank_account)) : new DefaultDb_Entity_BankAccount();
       			}*/
    	
    	$count->setBankName($post['name']);
      	$count->setAccountNum($post['idBank']);
    	$count->setClabe($post['clabe']);
    	$count->setBalance($post['balance']);
    	$count->setCompanie($user->getId());
    	$count->setUser($user->getId());
    	$count->setEmail($post['mail']);
    	$count->setUpgradeDate(date('Y-m-d H:m:s'));
    	$count->setNumBranch($post['numbranch']);
    	$count->setComments($post['comments']);
    	$count->setCity(1);
    	$count->setState(1);
    	$em->persist($count);
    	$em->flush();
    	$result="Datos Guardados.";
    	$this->view->result=json_encode($result);
    }
    //consult 
    public function getListBankAccountAction(){
    	
    	$this->view->setUseTemplate(FALSE);
    	header("Content-Type: application/json");
    
       	$em = $this->getEntityManager('DefaultDb');
       	$lista = $em->getRepository('DefaultDb_Entity_BankAccount')->findAll();
        	
    	$arrJson = array();
    	foreach($lista as $item)
    	{
    		$arrJson[] = array(
    			    "idBank" => $item->getId(),
    				"mail" => $item->getEmail(),
    				"name" => $item->getBankName(),
    				"number" => $item->getAccountNum(),
    				"clabe" => $item->getClabe(),
    				"numbranch" => $item->getNumBranch(),
    				"balance" => $item->getBalance(),
    				"upgrade" => substr($item->getUpgradeDate(), 0, 9),
    				"comments" => $item->getComments()
    		);
    	}
    
    	$this->view->result = json_encode($arrJson);
    }    
    
    public function delBankAccountAction()
      {
    	
      	$this->view->setUseTemplate(FALSE);
      	$post= $this->getRequest()->getPost();
      //$idcount= $post['idcount'];
      	$idcount= 3;
      	$em= $this->getEntityManager('DefaultDb'); 
      	//= $em->getRepository('DefaultDb_Entity_BankAccount')->findAll();
      	//foreach($banco as $item){
      		//echo $item->getBankName(); 
      //	} 
     //echo $banco->getBankName();
       $count= ($post["idcount"]) ? $em->getRepository('DefaultDb_Entity_BankAccount')->findOneBy(array('id_bank_account' =>$post["idcount"])) : new DefaultDb_Entity_BankAccount();
       $count->setBankName('Cambiado');
       $em->persist($count);
       
       $em->flush(); 
      
      }

      
     public function editBankAccountAction()
     {
    // 	$this->view->setUseTemplate(FALSE);
     	$this->view->dataRequest = NULL;
     	
     //	if( !is_null($this->getRequest()->getParam('idcount')) && is_numeric($this->getRequest()->getParam('idcount')) )
     	//{
     		$idReq = $this->getRequest()->getParam('idcount');
     		$em = $this->getEntityManager('DefaultDb');
            $cons = $em->getRepository('DefaultDb_Entity_BankAccount')->find($idReq);
            $this->view->id_bank_account=$idReq;
            $this->view->bank=$cons->getBankName();
     	    $this->view->date=date("d/m/Y", strtotime($cons->getUpgradeDate()));
     	    $this->view->numaccount=$cons->getAccountNum();
     	    $this->view->clbaccount=$cons->getClabe();
     	    $this->view->balance=$cons->getBalance();
     	    $this->view->numbranch=$cons->getNumBranch();
     	    $this->view->email=$cons->getEmail();
     	    $this->view->state=$cons->getState();
     	    $this->view->city=$cons->getCity();
     	    $this->view->comments=$cons->getComments();
     	   
     	   
     	   // $this->view->dataRequest = $em->getRepository('DefaultDb_Entity_BankAccount')->find($idReq);
          // echo "<input type='text' id='nameTF' name='nameTF' placeholder='' value='".$this->view->dataRequest->getBankName()."'/>";
          
           
     	    $lista = $em->getRepository('DefaultDb_Entity_State')->findAll();
     	    
     	    $combo = "";
     	    foreach($lista as $item)
     	    {
     	    	$combo .="<option value='".$item->getId();
     	    	$combo .="'>".$item->getName()."</option>\n";
     	    }
     	     
     	    $this->view->states = $combo;
        
           
     //	}
     } 
}
